package com.lizheng.yunservice.goods.controller;

import com.lizheng.yunservice.common.domain.ApiRestResponse;
import com.lizheng.yunservice.goods.model.VtpGoodsVo;
import com.lizheng.yunservice.goods.service.IVtpGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/vtp/app/goods")
public class VtpGoodsController {
    @Autowired
    private IVtpGoodsService ivtpGoodsService;

    /**
     * 增加商品转发数
     * @param goodsId 商品ID（Path 参数）
     * @param request HttpServletRequest（用于获取 Header）
     * @return ApiRestResponse
     */
    @PutMapping("/updateForwardNum/{goodsId}")  // 必须用 @PutMapping
    public ApiRestResponse<Void> updateForwardNum(
            @PathVariable String goodsId,  // 必须用 @PathVariable 获取 Path 参数
            HttpServletRequest request) {

        // 1. 检查 Authorization Header 是否存在
        String authHeader = request.getHeader("Authorization");
        if (authHeader == null || !authHeader.startsWith("Bearer ")) {
            return ApiRestResponse.fail("未提供有效的 Authorization Token");
        }

        // 2. 调用 Service 更新转发数
        return ivtpGoodsService.updateForwardNum(goodsId);
    }

    /**
     * 获取商品详情
     * @param id 商品ID
     * @return ApiRestResponse<VtpGoodsVo>
     */
    @GetMapping("/detail/{id}")
    public ApiRestResponse<VtpGoodsVo> getGoodsDetail(@PathVariable Long id) {
        VtpGoodsVo vo = ivtpGoodsService.getGoodsDetail(id);
        if (vo == null) {
            return ApiRestResponse.fail("商品不存在");
        }
        return ApiRestResponse.ok(vo);
    }

    /**
     * 获取推荐商品列表
     * @param limit 限制数量
     * @return ApiRestResponse<List<VtpGoodsVo>>
     */
    @GetMapping("/getGoodsRecomList")
    public ApiRestResponse<List<VtpGoodsVo>> getGoodsRecomList(@RequestParam(value = "limit", required = false) Integer limit) {
        List<VtpGoodsVo> list = ivtpGoodsService.getGoodsRecomList(limit);
        return ApiRestResponse.ok(list);
    }
}
